package org.seafloor.common.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport {
	private File upload;
	private String uploadFileName;
	private String uploadFileContentType;
	private String message = "成功上传文件";
	private String imagePath;

	public String execute() throws Exception {
		String path = ServletActionContext.getRequest().getRealPath("/upload");

		try {
			File file = this.getUpload();
			if (!this.getUploadFileName().endsWith(".jpg")) {
				message = "上传的文件格式只能为.jpg格式";
				return ERROR;
			}
			FileInputStream in = new FileInputStream(file);
			imagePath="upload/"+ this.getUploadFileName();
			FileOutputStream out = new FileOutputStream(path + "\\"+ this.getUploadFileName());
			byte[] b = new byte[1024];
			int length = 0;
			while ((length = in.read(b)) != -1) {
				out.write(b, 0, length);
			}
			in.close();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			message = "上传文件失败，请联系管理员";
		}
		return SUCCESS;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadFileContentType() {
		return uploadFileContentType;
	}

	public void setUploadFileContentType(String uploadFileContentType) {
		this.uploadFileContentType = uploadFileContentType;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getImagePath() {
		return imagePath;
	}

	public void setImagePath(String imagePath) {
		this.imagePath = imagePath;
	}

}
